What is docker-modem?
The docker-modem npm package is a low-level Docker API client for Node.js. It allows developers to interact with Docker's REST API programmatically, enabling them to manage Docker containers, images, networks, and more directly from their Node.js applications.
What are docker-modem's main functionalities?
Managing Containers
This feature allows you to create and manage Docker containers. The code sample demonstrates how to create a new container using the 'alpine' image and run a simple command inside it.
const Docker = require('docker-modem');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
const createContainer = async () => {
const opts = {
path: '/containers/create',
method: 'POST',
options: {
Image: 'alpine',
Cmd: ['echo', 'Hello World']
}
};
docker.dial(opts, (err, data) => {
if (err) throw err;
console.log(data);
});
};
createContainer();
Managing Images
This feature allows you to manage Docker images. The code sample demonstrates how to pull the 'alpine' image from Docker Hub.
const Docker = require('docker-modem');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
const pullImage = async () => {
const opts = {
path: '/images/create',
method: 'POST',
options: {
fromImage: 'alpine',
tag: 'latest'
}
};
docker.dial(opts, (err, data) => {
if (err) throw err;
console.log(data);
});
};
pullImage();
Managing Networks
This feature allows you to manage Docker networks. The code sample demonstrates how to create a new network with the 'bridge' driver.
const Docker = require('docker-modem');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
const createNetwork = async () => {
const opts = {
path: '/networks/create',
method: 'POST',
options: {
Name: 'my_network',
Driver: 'bridge'
}
};
docker.dial(opts, (err, data) => {
if (err) throw err;
console.log(data);
});
};
createNetwork();
Other packages similar to docker-modem
dockerode
Dockerode is a high-level Docker API client for Node.js. It provides a more user-friendly interface compared to docker-modem and includes additional abstractions to simplify common tasks. While docker-modem is more low-level and closer to the raw Docker API, dockerode offers a more convenient and higher-level API for managing Docker resources.
node-docker-api
Node-docker-api is another Docker API client for Node.js. It offers a balance between low-level control and high-level convenience. It provides a promise-based interface, making it easier to work with asynchronous operations compared to docker-modem's callback-based approach.
docker-modem
Docker's Remote API network layer module.
docker-modem
will help you talking with Docker
, since it already implements all the network strategies needed to support all Docker
's Remote API endpoints.
It is the module powering (network wise) dockerode and other modules.
Usage
Getting started
var Modem = require('docker-modem');
var modem1 = new Modem({socketPath: '/var/run/docker.sock'});
var modem2 = new Modem();
var modem3 = new Modem({host: 'http://192.168.1.10', port: 3000});
var modem4 = new Modem({protocol:'http', host: '127.0.0.1', port: 3000});
var modem5 = new Modem({host: '127.0.0.1', port: 3000});
SSH
You can connect to the Docker daemon via SSH in two ways:
- Using the built-in SSH agent.
- Implement your own custom agent.
var modem1 = new Modem({
prococol: 'ssh',
host: 'ssh://127.0.0.1',
port: 22
});
var customAgent = myOwnSSHAgent({host: 'ssh://127.0.0.1', port: 22});
var modem2 = new Modem({
agent: customAgent,
});
Tests
- Tests are implemented using
mocha
and chai
. Run them with npm test
. - Check dockerode tests, which is indirectly co-testing
docker-modem
.
License
Pedro Dias - @pedromdias
Licensed under the Apache license, version 2.0 (the "license"); You may not use this file except in compliance with the license. You may obtain a copy of the license at:
http://www.apache.org/licenses/LICENSE-2.0.html
Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "as is" basis, without warranties or conditions of any kind, either express or implied. See the license for the specific language governing permissions and limitations under the license.